package cn.eng.system.controller;

import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.PageList;
import cn.eng.system.domain.Dictionarytype;
import cn.eng.system.query.DictionarytypeQuery;
import cn.eng.system.service.IDictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口: 数据字典
 */
@RestController
@RequestMapping("/dictionarytype")
public class DictionarytypeController {
        @Autowired
        private IDictionarytypeService dictionarytypeService;

    /**
     * 查询所有对象
     * @return
     */
    @GetMapping
        public List<Dictionarytype> loadAll(){
           return dictionarytypeService.loadAll();
        }
    /**
     * 查询单个对象
     * @return
     */
    @GetMapping("/{id}")
    public Dictionarytype loadById(@PathVariable("id") Long id){
        return dictionarytypeService.loadById(id);
    }

    /**
     * 分页查询
     * @param dictionarytypeQuery
     * @return
     */
   @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery dictionarytypeQuery){
        return dictionarytypeService.queryPage(dictionarytypeQuery);
    }

    /**
     * 单个删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
     try {
         dictionarytypeService.remove(id);
         return new AjaxResult();
      } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"删除失败");
      }
    }

    /**
     * 添加或修改接口
     * @param dictionarytype
     * @return
     */
   @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if (dictionarytype.getId()==null){
                dictionarytypeService.save(dictionarytype);
            }else {
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
       try {
           dictionarytypeService.patchDel(ids);
           return  new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
        return new AjaxResult(false,"批量删除失败");
        }

    }
}
